/*
 * EmptySource.java
 *
 * Created on July 17, 2003, 6:50 PM
 */

package org.infoset.xml.util;

import org.infoset.xml.*;

/**
 *
 * @author  alex
 */
public class EmptySource implements ItemSource {
   
   /** Creates a new instance of EmptySource */
   public EmptySource() {
   }
   
   public Item receive() 
      throws XMLException
   {
      return null;
   }
   
   public void flush(boolean all) 
      throws XMLException
   {
      if (all) {
         Item i;
         while ((i=receive())!=null);
         return;
      }
      int level = 1;
      Item i = receive();
      while (level>0 && ((i=receive())!=null)) {
         if (i.getType()==Item.ItemType.ElementEndItem || i.getType()==Item.ItemType.DocumentEndItem) {
            level--;
         } else if (i.getType()==Item.ItemType.ElementItem || i.getType()==Item.ItemType.DocumentItem) {
            level++;
         }
      }
   }

   public Element seek(Name name) 
      throws XMLException
   {
      Element found = null;
      Item i;
      while (found==null && (i=receive())!=null && i.getType()!=Item.ItemType.ElementEndItem) {
         if (i.getType()==Item.ItemType.ElementItem ) {
            Element e = (Element)i;
            if (e.getName().equals(name)) {
               found = e;
            } else {
               flush(false);
            }
         }
      }
      return found;
   }

   public Item seekByType(Item.ItemType type) 
      throws XMLException
   {
      Item i;
      while ((i=receive())!=null && i.getType()!=type);
      return i;
   }
   
   public void copyTo(ItemDestination dest)
      throws XMLException
   {
      Item i;
      while ((i=receive())!=null) {
         dest.send(i);
      }
   }
}
